<template>
  <el-table
    :data="permissionArr"
    style="width: 100%; margin-bottom: 20px"
    row-key="id"
    border
  >
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限值" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作">
      <template #="{ row }">
        <el-button
          type="primary"
          size="small"
          :disabled="row.level == 4 ? true : false"
          @click="addPermission(row)"
          >{{ row.level == 3 ? '添加功能' : '添加菜单' }}</el-button
        >
        <el-button
          type="warning"
          size="small"
          :disabled="row.level == 1 ? true : false"
          @click="updatePermission(row)"
          >编辑</el-button
        >
        <el-button
          type="danger"
          size="small"
          :disabled="row.level == 1 ? true : false"
          @click="deletePermission(row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <el-dialog
    v-model="dialogVisible"
    :title="permissionData.id ? '修改权限' : '添加权限'"
    width="500"
  >
    <template #default>
      <el-form label-width="80px">
        <el-form-item label="名称" style="width: 250px">
          <el-input v-model="permissionData.name"></el-input>
        </el-form-item>
        <el-form-item label="权限值" style="width: 250px">
          <el-input v-model="permissionData.code"></el-input>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import {
    reqAddOrUpdatePermission,
    reqAllPermissionList,
    reqDeletePermission,
  } from '@/api/acl/permission'
  import type {
    AllPermissionInfo,
    AllPermissionList,
    PermissionData,
  } from '@/api/acl/permission/type'
  import { ElMessage } from 'element-plus'
  import { ref, onMounted, reactive } from 'vue'

  let permissionArr = ref<AllPermissionList>([])
  let dialogVisible = ref<boolean>(false) //控制新增/修改权限弹框的显示状态
  let permissionData = reactive<PermissionData>({
    code: '',
    level: -1,
    name: '',
    pid: -1,
  })

  onMounted(() => {
    getHasPermissionList()
  })

  const getHasPermissionList = async () => {
    let result = await reqAllPermissionList()
    if (result.code === 200) {
      permissionArr.value = result.data
    } else {
      ElMessage({ type: 'error', message: '获取菜单信息失败，请刷新重试' })
    }
  }

  //添加菜单/功能按钮的事件回调
  const addPermission = (row: AllPermissionInfo) => {
    //清除上次打开后留存的信息
    permissionData.id = 0
    permissionData.name = ''
    permissionData.code = ''
    //记录已有信息
    permissionData.level = row.level + 1
    permissionData.pid = row.id
    dialogVisible.value = true
  }
  //弹框确认按钮事件回调
  const save = async () => {
    let result = await reqAddOrUpdatePermission(permissionData)
    if (result.code === 200) {
      ElMessage({
        type: 'success',
        message: permissionData.id ? '修改权限成功' : '添加权限成功',
      })
      getHasPermissionList()
      dialogVisible.value = false
    } else {
      ElMessage({
        type: 'error',
        message: permissionData.id ? '修改权限失败' : '添加权限失败',
      })
    }
  }

  //编辑按钮事件回调
  const updatePermission = (row: AllPermissionInfo) => {
    Object.assign(permissionData, row)
    dialogVisible.value = true
  }
  //删除按钮事件回调
  const deletePermission = async (row: AllPermissionInfo) => {
    let result = await reqDeletePermission(row.id)
    if (result.code === 200) {
      ElMessage({ type: 'success', message: '删除权限成功' })
      getHasPermissionList()
    } else {
      ElMessage({ type: 'error', message: '删除权限失败' })
    }
  }
</script>

<style scoped></style>
